import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { NbToastrService } from '@nebular/theme';
import { UserService } from '@nx-chat/common/service';
import { UserDto, UidsDto } from '@nx-chat/common/dto';

@Component({
  selector: 'view-friend',
  templateUrl: './view-friend.component.html',
  styleUrls: ['./view-friend.component.scss']
})
export class ViewFriendComponent implements OnInit {

  /**好友邮箱 */
  public email: string;
  /**是否为好友 */
  public isFriend: boolean;

  public friend: UserDto;

  constructor(
    private _userService: UserService,
    private _router: Router,
    private _activateRoute: ActivatedRoute,
    private _nbToastrService: NbToastrService,
  ) {}

  public getChat() {
    this._router.navigate([`/message/${this.friend.uid}`]);
  }

  public getExplore() {}

  ngOnInit() {
    const uid = this._userService.uId;
    const uids = {} as UidsDto;
    this._activateRoute.paramMap.subscribe((url) => {
      this.email = url.get('id');
      this._userService.getUserByEmail(this.email).subscribe(user => {
        if (user) {
          this.friend = user;
          uids.uid = uid;
          uids.friendId = user.uid;
          this._userService.isFriend(uids).subscribe(res => this.isFriend = res.isFriend);
        } else {
          this._nbToastrService.danger('', '该用户不存在！');
          this._router.navigate(['/friend']);
        }
      })
    });
  }
}
